/**
 * @ author: 汪孟苹
 */
define(["jquery", "template", "bbcare", "oms_config/root"], function ($, template, $bb, $root) {
	var servers = {}
	/**
	 * 获取url地址,当前用户
	 */
	var URL = $bb.baseUrl("followup")
	var sessinonUserName = JSON.parse($bb.getSessionStorage("account")).alias
	/**
	 * 请求队列数据
	 */
	servers.ajaxQueueList = function (callback) {
		var config = $root.init()
		var showFlag = config.dom.addPatientQueueshowFlag ? "1" : "0"
		$bb.ajax({
			url: URL + "followup/service/queue/followQueueListQuery",
			data: JSON.stringify({
				data: { "showFlag": showFlag }
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data)
				}
			}
		})
	}
	/**
	 * 处理队列数据
	 */
	servers.handleQueues = function (list) {
		var arr = []
		for (var i = 0; i < list.length; i++) {
			arr[i] = list[i]
			arr[i].flag = false
			if (arr[i].styleLogo == "" || arr[i].styleLogo == null) {
				arr[i].styleLogo = "fa fa-tags"
			}
		}
		arr[0].flag = true
		return arr
	}
	/**
	 * 获取初始化参数（第一个队列的相关信息）
	 */
	servers.getInitQueueParams = function (data) {
		var curQueue = data.rows[0]
		return {
			"queueName": curQueue.queueName,
			"queueId": curQueue.id,
			"schemeId": curQueue.schemeId
		}
	}
	/**
	 * 请求建档问卷
	 */
	servers.ajaxBuildQuestions = function (params, callback, values) {
		$bb.ajax({
			url: URL + "followup/service/queue/patientAddContentQuery",
			data: JSON.stringify({
				data: {
					"followQueueId": params.queueId,
					"followSchemeId": params.schemeId
				}
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data, params, values)
				} else {
					$bb.toastShow(data.msg ? data.msg : queueName + "建档问卷添加失败！")
				}
			}
		})
	}
	/**
	 * 请求保存建档信息
	 */
	servers.ajaxSaveBuildQuestions = function (params, patientName, questiones, callback) {
		$bb.ajax({
			url: URL + "followup/service/patient/patientAdd",
			data: JSON.stringify({
				data: {
					"followQueueId": params.queueId,
					"opCode": "1000",
					"opNote": "【" + sessinonUserName + "】医生添加患者【" + patientName + "】建档信息，加入队列" + params.queueName,
					"opType": "2",
					"userType": "1",
					"sourceType": "2",
					"questiones": questiones
				}
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data)
				} else {
					$bb.toastShow(data.msg ? data.msg : "添加失败！")
				}
			}
		})
	}
	/**
	 * 根据患者实例id请求患者基本信息
	 */
	servers.ajaxPatientInfo = function (params, callback) {
		$bb.ajax({
			url: URL + "followup/service/patient/patientInfoQueryByPtId",
			type: "post",
			contentType: "application/json;charset=UTF-8",
			dataType: "json",
			data: JSON.stringify({
				data: {
					"patientId": params.patientId,
					"ptSchemeId": params.ptSchemeId
				}
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data)
				} else {
					$bb.toastShow(data.msg ? data.msg : "跳转失败！")
				}
			}
		})
	}

	/**
		 * 根据页面传递过来的队列id获取当前应展示第i个
		 */
	servers.getCurQueue = function (list, followQueueId) {
		for (var i = 0; i < list.length; i++) {
			if (list[i].id == followQueueId) {
				return i
			}
		}
	}

	/**
	 * 绑定 his id 和 PatientId 的关联
	 */
	servers.bindHisIdAndPatientId = function (data, hisId, callback) {
		$bb.ajax({
			url: URL + "followup/webapi/his/save_followupid_hisid?followup_id=" + data.patientId + '&his_id=' + hisId,
			type: 'post',
			contentType: "application/json;charset=UTF-8",
			dataType: "json",
			success: function (res) {
				if (res.success) {
					callback(res.data)
				} else {
					$bb.toastShow(res.msg ? res.msg : "绑定失败！")
				}
			}
		})
	}

	/**
		 * 根据母亲手机号请求数据
		 */
	servers.ajaxQueryFamilyInfo = function (followQueueId, phone, callback) {
		// console.log('params', followQueueId)
		$bb.ajax({
			url: URL + "followup/gy3y/queryFamilyInfo",
			data: JSON.stringify({
				data: {
					queueId: followQueueId,
					phone: phone
				}
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data)
				} else {
					$bb.toastShow(data.msg ? data.msg : "请求失败！")
				}
			}
		})
	}

	return servers
})
